package com.ys.nms.module.business.service.ipv4Mac;

import cn.hutool.core.collection.CollUtil;
import com.ys.nms.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.ys.nms.module.business.controller.admin.ipv4Mac.vo.*;
import com.ys.nms.module.business.dal.dataobject.ipv4Mac.Ipv4MacDO;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.util.object.BeanUtils;

import com.ys.nms.module.business.dal.mysql.ipv4Mac.Ipv4MacMapper;

import static com.ys.nms.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.convertList;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.diffList;
import static com.ys.nms.module.business.enums.ErrorCodeConstants.*;

/**
 * IPMAC Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class Ipv4MacServiceImpl implements Ipv4MacService {

    @Resource
    private Ipv4MacMapper ipv4MacMapper;

    @Override
    public Long createIpv4Mac(Ipv4MacSaveReqVO createReqVO) {
        // 插入
        Ipv4MacDO ipv4Mac = BeanUtils.toBean(createReqVO, Ipv4MacDO.class);
        ipv4MacMapper.insert(ipv4Mac);

        // 返回
        return ipv4Mac.getId();
    }

    @Override
    public void updateIpv4Mac(Ipv4MacSaveReqVO updateReqVO) {
        // 校验存在
        validateIpv4MacExists(updateReqVO.getId());
        // 更新
        Ipv4MacDO updateObj = BeanUtils.toBean(updateReqVO, Ipv4MacDO.class);
        ipv4MacMapper.updateById(updateObj);
    }

    @Override
    public void deleteIpv4Mac(Long id) {
        // 校验存在
        validateIpv4MacExists(id);
        // 删除
        ipv4MacMapper.deleteById(id);
    }

    @Override
    public void deleteIpv4MacListByIds(List<Long> ids) {
        // 删除
        ipv4MacMapper.deleteByIds(ids);
    }


    private void validateIpv4MacExists(Long id) {
        if (ipv4MacMapper.selectById(id) == null) {
            throw exception(IPV4_MAC_NOT_EXISTS);
        }
    }

    @Override
    public Ipv4MacDO getIpv4Mac(Long id) {
        return ipv4MacMapper.selectById(id);
    }

    @Override
    public PageResult<Ipv4MacDO> getIpv4MacPage(Ipv4MacPageReqVO pageReqVO) {
        return ipv4MacMapper.selectPage(pageReqVO);
    }

    @Override
    public List<Ipv4MacDO> getIpv4MacList(Ipv4MacPageReqVO pageReqVO) {
        LambdaQueryWrapperX<Ipv4MacDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eqIfPresent(Ipv4MacDO::getPortId, pageReqVO.getPortId())
                .eq(Ipv4MacDO::getDeviceId, pageReqVO.getDeviceId())
                .eqIfPresent(Ipv4MacDO::getMacAddress, pageReqVO.getMacAddress())
                .eqIfPresent(Ipv4MacDO::getIpv4Address, pageReqVO.getIpv4Address());
        return ipv4MacMapper.selectList(wrapperX);
    }

}